<?php

/**
 * 任务模块的接口类
 * 接口返回值都采用json格式
 * @author chacha
 * @date 2016-2-1
 */
class TaskapiAction extends TeamBaseAction
{
    private $TASK_STATUS = array('删除', '未开始', '进行中', '已提交', '已验收');

    private function auth_verify($taskid)
    {
        $tabTask = M('prjtask');
        $res = $tabTask->where("id=" . $taskid . " and prjid=" . $this->userinfo['projectid'])->find();
        if (!$res) {
            ajaxf("找不到这条任务！");
        }
    }

    //添加任务
    /**
     *
     */
    public function addTask()
    {
        $title = I('title');
        $content = I('content', '');
        $to = I('to');
        $cc = I('cc');
        $deadline = I('deadline');
        $flag = I('flag'); //紧急程度
        if (!$title || !$to || !$deadline) {
            ajaxf("参数错误！");
        }

        $level = \App\Common\Constvars::TASK_FLAG_NORMAL;
        if ($flag == 1) {
            $level = \App\Common\Constvars::TASK_FLAG_EMGER;
        }

        $from = $this->uid;
        $prjid = $this->userinfo['projectid']; //默认发任务者所在项目就是任务所属项目
        $process = \App\Common\Constvars::TASK_PROC_UNTAKEN;
        $status = \App\Common\Constvars::STATUS_NORMAL;
        $data = array(
            'prjid' => $prjid,
            'fromwho' => $from,
            'exer' => $to,
            'title' => $title,
            'content' => $content,
            'deadline' => strtotime($deadline),
            'level' => $level,
            'process' => $process,
            'status' => $status,
        );

        $tabTask = M('prjtask');
        //校验雷同标题
        $res = $tabTask->where("title='" . $title . "' and prjid=" . $prjid)->find();
        if ($res) {
            ajaxf("已发布有相同标题的任务，请更换新的标题！");
        }
        $res = $tabTask->add($data);
        if ($res) {
            ajaxs(1, $res);
        }
        ajaxf("添加任务失败！请重试！");
    }

    //编辑任务信息
    public function editTask()
    {
        //权限校验


        //前端做内容变化校验
        $taskid = I('task');
        if (!$taskid) {
            ajaxf("参数错误！");
        }
        $this->auth_verify($taskid);
        $tabTask = M('prjtask');

        $title = I('title'); //标题
        if ($title) {
            $veriTitle = $tabTask->where("title='" . $title . "' and prjid=" . $this->userinfo['projectid'])->find();
            if ($veriTitle && $veriTitle['id'] != $taskid) {
                ajaxf("已有一个标题为\"" . $title . "\"的任务，请使用一个不重复的标题！");
            }
            $res['title'] = $title;
        }
        $content = I('content'); //内容
        $deadline = I('deadline'); //截止日期
        $level = I('level'); //紧急程度
        if ($content) {
            $res['content'] = $content;
        }
        if ($deadline) {
            $res['deadline'] = strtotime($deadline);
        }
        if ($level) {
            $res['level'] = $level;
        }
        $ret = $tabTask->save($res);
        if ($res !== false) {
            ajaxs();
        } else {
            ajaxf("保存任务信息失败！");
        }
    }

    public function saveTask()
    {
        $taskId = intval(I("id"));
        $title = I("title");
        $content = I("content");
        $to = I("to");
        $cc = I("cc");

        $deadline = I("deadline");
        $priority = I("priority");
        if (empty($title) || empty($content) || empty($to) || empty($deadline)) {
            ajaxf("请确认填写完整");
        }

        $table = M('prjtask');
        if ($taskId>0) {
            $task = $table->where("id=$taskId")->find();
            if ($task['fromwho']!=$this->uid) {
                ajaxf("您没有权限修改任务内容");
            }
        }
        //暂存原数据
        $originTask = false;
        $oriTaskMention = false;
        $projectId = $this->userinfo['projectid'];
        $where = "title= '$title' and prjid=$projectId and status>0";
        if ($taskId > 0) {
            $where .= "id!=$taskId";
        }
        $task = $table->where($where)->find();
        if ($task) {
            $originTask = $table->where("id=$taskId and status>0")->find();
            ajaxf("已有一个标题为\"$title\"的任务，请使用一个不重复的标题！");
        }
        
        if (!$task) {
            $task = array('id'=>$taskId);
        }
        $task["title"] = $title;
        $task["content"] = $content;
        $task["fromwho"] = $this->uid;
        $task["exer"] = $to;
        $task["deadline"] = $deadline;
        $task["level"] = $priority;
        //通知消息数组
        $allogs = array();
        $now = time();

        $tableTaskat = M('taskat');
        
        //执行者站内信和邮件通知处理闭包
        $exerNoticer = function(&$logarr, $task, $uid, $userinfo, $ct){
            //站内通知
            $notice = "<td>用户<a href='" . genTalentURL($uid) . "'><b>[" . $userinfo['nickname'] . "]</b></a>向您分派了任务[<b>".$task['title']."</b>]，请尽快领取。</td>";
            $notice = logPost($notice, U("Task/main@".getDomain())."#tabMyTasks", $ct, "去看看");
            pushLog($logarr, $task['exer'], $notice, $ct);
            //邮件
            $resU = M('user')->where('id='.$task['exer'])->find();
            //3天未登陆则发邮件通知
            if ($resU && $resU['lastime']< time()-\App\Common\Constvars::MAIL_NOTICE_TIME) {
                $body = "用户[" . $userinfo['nickname'] . "]向您分派了一个任务".$task['title']."]，请尽快登陆<a href='".U("Task/main@".getDomain())."#tabMyTasks'>般若虾-虚拟办公室</a>领取。";
                $body = mailPostfix($body);
                $resUser = M('user')->where("id=".$task['exer'])->join('xia_user_info uif on xia_user.id=uif.uid')->find();
                mailqueue_push($resUser['email'], $resUser['nickname'], '您有一个任务', $body);
            }
        };

        if ($taskId <= 0) {
            $task["status"] = 1;
            $task["process"] = 1;
            $task["prjid"] = $projectId;
            //taskId被重新赋值
            $taskId = $table->add($task);
            if ($taskId<=0) {
                ajaxf("添加任务失败，请重试。");
            }
            $exerNoticer($allogs, $task, $this->uid, $this->userinfo, $now);
        } else {
            $table->save($task);
            //站内通知
            //发布者可以修改执行者，如果执行者更换，通知新的执行者
            if ($originTask['exer']!=$task['exer']) {
                $exerNoticer($allogs, $task, $this->uid, $this->userinfo, $now);
            }
            $oriTaskMention = $tableTaskat->where("taskid=$taskId")->select();
            $tableTaskat->where("taskid=$taskId")->delete();
        }

        if ($taskId > 0) {
            //通知被@用户
            $oriMentions = array();
            $oriMentions = array_reduce($oriTaskMention, function($v,$w){
                $v[$w['id']]=$w['towho'];
                return $v;
            });
            
            $newRows = array();
            $newMentions = array();
            foreach ($cc as $ccid) {
                $newRows[] = array("taskid" => $taskId, "fromwho" => 0, "towho" => $ccid);
                if (!in_array($ccid, $oriMentions)) {
                    $newMentions[] = $ccid;
                }
            }
            $ret = $tableTaskat->addAll($newRows);
            if ($ret) {
                $where = array('id'=>array('in', $newMentions));
                $join = 'LEFT JOIN xia_user_info uif ON xia_user.id=uif.uid';
                $resMentUsers = M('user')->join($join)->where($where)->select();
                if (is_array($resMentUsers)) {
                    foreach ($resMentUsers as $mu) {
                        //拼装站内通知
                        $notice = "<td>用户<a href='" . genTalentURL($this->uid) . "'><b>[" . $this->userinfo['nickname'] . "]</b></a>在任务[<b>".$title."</b>]中提到了您，请留意。</td>";
                        $notice = logPost($notice, U("Task/main@".getDomain())."#tabFollowMe", $now, "去看看");
                        pushLog($allogs, $mu['id'], $notice, $now);
                        //3天未登陆则发邮件通知
                        if ($mu['lastime']< $now-\App\Common\Constvars::MAIL_NOTICE_TIME) {
                            $body = "[" . $this->userinfo['nickname'] . "]在任务[$title]中提到了您，请尽快登陆<a href='".U("Task/main@".getDomain())."#tabFollowMe'>般若虾-虚拟办公室</a>查看。";
                            $body = mailPostfix($body);
                            $resUser = M('user')->where("id=$to")->join('xia_user_info uif on xia_user.id=uif.uid')->find();
                            mailqueue_push($mu['email'], $mu['nickname'], '您有一个任务', $body);
                        }
                    }
                }
            }
        }
        //统一将站内通知入库
        M('office_log')->addAll($allogs);
        ajaxs();
    }

    public function deleteTask()
    {
        $taskid = I("id");
        $table = M("prjtask");
        $task = $table->where("id=$taskid")->find();
        $ret = $table->where("id=" . $taskid)->save(array('status'=>0));
        if ($ret===false) {
            ajaxf();
        }
        
        $now = time();
        $logarr = array();
        $noticer = function(&$arr, $uid, $userinfo, $to, $title, $ct, $totype){
            $notice = "";
            if ($totype==1) {
                $notice = "<td>分派给您的任务[<b>".$title."</b>]已被用户<a href='" . genTalentURL($uid) . "'><b>[" . $userinfo['nickname'] . "]</b></a>删除，请留意。</td>";
            }
            else if ($totype==2) {
                $notice = "<td>您在其中被提到的任务[<b>".$title."</b>]已被用户<a href='" . genTalentURL($uid) . "'><b>[" . $userinfo['nickname'] . "]</b></a>删除，请留意。</td>";
            }
            
            $notice = logPost($notice, U("Task/main@".getDomain()), $ct, "去看看");
            pushLog($arr, $to, $notice, $ct);
        };
        $mailer = function($beMentionedUser, $uif, $title){
            if ($beMentionedUser['lastime']<time()-\App\Common\Constvars::MAIL_NOTICE_TIME) {
                $body = "用户[" . $uif['nickname'] . "]删除了任务[$title]，请登陆<a href='".U("Task/main@".getDomain())."'>般若虾-虚拟办公室</a>了解。";
                $body = mailPostfix($body);
                mailqueue_push($beMentionedUser['email'], $beMentionedUser['nickname'], '您有一个任务被删除', $body);
            }
        };
        
        //执行者
        //站内通知
        $noticer($logarr, $this->uid, $this->userinfo, $task['exer'], $task['title'], $now, 1);
        //是否邮件通知
        $bmUser = M('user')->join('xia_user_info uif on xia_user.id=uif.uid')->where("id=".$task['towho'])->find();
        $mailer($bmUser, $this->userinfo, $task['title']);
        
        $joinUser = "left join xia_user u on xia_taskat.towho=u.id";
        $joinUif = "left join xia_user_info uif on xia_taskat.towho=uif.uid";
        $field = "u.email,u.lastime,uif.uid,uif.nickname";
        $bemedList = M('taskat')->field($field)->join($joinUser)->join($joinUif)->where("taskid=$taskid")->select();
        if ($bemedList) {
            //删除被@的人
            M('taskat')->where("taskid=$taskid")->delete();
            //通知被艾特的人
            foreach ($bemedList as $bm) {
                $noticer($logarr, $this->uid, $this->userinfo, $bm['uid'], $task['title'], $now, 2);
                $mailer($bm, $this->userinfo, $task['title']);
            }
        }
        M('office_log')->addAll($logarr);
        ajaxs();
    }

    //获取一条任务数据
    public function getSingleTask()
    {
        $taskid = I('task');
        $this->auth_verify($taskid);
        $tabTask = M('prjtask');

        $table = array('xia_prjtask' => 't');
        $field = "t.*,u1.nickname as sender,u2.nickname as exetor,";
        $field .= "(SELECT GROUP_CONCAT(towho) FROM xia_taskat WHERE t.id = xia_taskat.taskid) as cc ,";
        $field .= "(SELECT GROUP_CONCAT(xia_user_info.nickname) From xia_taskat
        LEFT JOIN xia_user
        ON xia_user.id=xia_taskat.towho
        LEFT JOIN xia_user_info
        ON xia_user.id= xia_user_info.uid
        WHERE t.id = xia_taskat.taskid) as ccNames ";
        $join = "LEFT JOIN xia_user_info u1 ON t.fromwho=u1.uid LEFT JOIN xia_user_info u2 ON t.exer=u2.uid";
        $res = $tabTask->table($table)->field($field)->join($join)->where('t.id=' . $taskid.' and t.status>0')->select();

        $result = array();
        if ($res) {
            foreach ($res as $item) {
                $row = array(
                    "id" => $item["id"],
                    "projectId" => $item["prjid"],
                    "from" => $item["fromwho"],
                    "fromName" => $item["sender"],
                    "to" => $item["exer"],
                    "toName" => $item["exetor"],
                    "status" => $item["status"],
                    "priority" => $item["level"],
                    "deadline" => $item["deadline"],
                    "title" => $item["title"],
                    "content" => $item["content"],
                    "cc" => mb_split(",", $item["cc"]),
                    "ccName" => mb_split(",", $item["ccNames"])
                );
                $result[] = $row;
            }
        }
        ajaxs(1, '', $result);
    }

    //艾特
    public function setAtlist()
    {
        $taskid = I('task');
        $this->auth_verify($taskid);
        $uidstr = I('users');
        $uidarr = explode(',', $uidstr);
        $tabTaskat = M('taskat');
        $atlist = array();
        $res = $tabTaskat->where("taskid=" . $taskid)->getField("towho", true);
        //已有艾特用户
        if ($res) {
            foreach ($uidarr as $at) {
                if (!in_array($at, $res)) {
                    array_push($atlist, array('taskid' => $taskid, 'fromwho' => $this->uid, 'towho' => $at));
                }
            }
        } //无艾特用户
        else {
            foreach ($uidarr as $uid) {
                array_push($atlist, array('taskid' => $taskid, 'fromwho' => $this->uid, 'towho' => $uid));
            }
        }
        //插入数据库
        if (count($atlist) > 0) {
            $res = $tabTaskat->addAll($atlist);
            if ($res === false) {
                ajaxf("存储失败！");
            } else {
                ajaxs();
            }
        } else {
            ajaxs();
        }
    }

    //获取一条任务艾特数据
    public function getAtlist()
    {
        $taskid = I('task');
        $this->auth_verify($taskid);
        $tabAt = M('taskat');
        $table = array('xia_taskat' => 'ta');
        $field = "ta.taskid,ta.towho,uif.nickname";
        $join = "LEFT JOIN xia_user_info uif ON ta.towho=uif.uid";
        $res = $tabAt->table($table)->field($field)->join($join)->where("ta.taskid=" . $taskid)->select();
        if ($res === false) {
            ajaxf('数据错误！');
        } else {
            ajaxs(1, '', $res);
        }
    }

    //获取任务列表
    public function getTaskList()
    {
        $tasktype = I('type'); //任务类型,1我发布的，2分派给我的，3艾特我的
        $where = 't.prjid=' . $this->userinfo['projectid']." and t.status>0";
        $res = false;
        if ($tasktype == 3) {
            $tabTaskAt = M('taskat');
            $table = array('xia_taskat' => 'ta', 'xia_prjtask' => 't');
            $field = "t.id, t.title,t.deadline,t.content,t.process,t.status,t.level,u1.nickname as sender,u2.nickname as exetor";
            $join = "LEFT JOIN xia_user_info u1 ON t.fromwho=u1.uid LEFT JOIN xia_user_info u2 ON t.exer=u2.uid";
            $where = "t.status>0 and ta.towho=" . $this->uid . " and ta.taskid=t.id";
            $res = $tabTaskAt->table($table)->field($field)->join($join)->where($where)->select();
        } else {
            $tabTask = M('prjtask');
            if ($tasktype == 1) {
                $table = array('xia_prjtask' => 't');
                $field = "t.id,t.title,t.deadline,t.process,t.level,t.status,uif.nickname as exetor";
                $join = "LEFT JOIN xia_user_info uif ON t.exer=uif.uid";
                $where .= " and t.fromwho=" . $this->uid;
            } else if ($tasktype == 2) {
                $table = array('xia_prjtask' => 't');
                $field = "t.id,t.title,t.deadline,t.process,t.level,t.status,uif.nickname as sender";
                $join = "LEFT JOIN xia_user_info uif ON t.fromwho=uif.uid";
                $where .= " and t.exer=" . $this->uid;
            }
            $res = $tabTask->table($table)->field($field, true)->join($join)->where($where)->select();
        }
        if ($res === false) {
            ajaxf("数据错误！");
        } else {
            //处理状态标签数组
            //我发布的
            if ($tasktype==1) {
                for ($i = 0; $i < count($res); $i++) {
                    if ($res[$i]['process']==1) {
                        $res[$i]['statag'] = array(1);
                    }
                    else if ($res[$i]['process']==2) {
                        $res[$i]['statag'] = array(2,3,4);
                    }
                    else if ($res[$i]['process']==3) {
                        $res[$i]['statag'] = array(3,4);
                    }
                    else if ($res[$i]['process']==4) {
                        $res[$i]['statag'] = array(4);
                    }
                }
            }
            else if ($tasktype==2) {
                for ($i = 0; $i < count($res); $i++) {
                    if ($res[$i]['process']==1) {
                        $res[$i]['statag'] = array(1,2);
                    }
                    else if ($res[$i]['process']==2) {
                        $res[$i]['statag'] = array(2,3);
                    }
                    else if ($res[$i]['process']==3) {
                        $res[$i]['statag'] = array(3,4);
                    }
                    else if ($res[$i]['process']==4) {
                        $res[$i]['statag'] = array(4);
                    }
                }
            }
            
            ajaxs(1, '', $res);
        }
    }

    //添加评论
    public function comment()
    {
        $taskid = I('task');
        $this->auth_verify($taskid);
        $comment = I('text');
        if (!$comment) {
            ajaxf("参数错误！");
        }
        $tabTaskComment = M('taskcomment');
        $data = array(
            'taskid' => $taskid,
            'author' => $this->uid,
            'ctime' => time(),
            'content' => $comment,
            'status' => 1,
        );
        $res = $tabTaskComment->add($data);
        if ($res) {
            ajaxs(1, '', $res);
        } else {
            ajaxf('添加评论失败！');
        }
    }

    //获取一条任务的所有评论
    public function getTaskComments()
    {
        $taskid = I('task');
        $this->auth_verify($taskid);
        $tabTaskComment = M('taskcomment');
        $table = array('xia_taskcomment' => 'tc');
        $field = "tc.id,tc.author,tc.taskid,tc.status,tc.content,from_unixtime(tc.ctime,'%Y-%m-%d %H:%i:%s') as ctime ,uif.nickname as authorname";
        $join = "LEFT JOIN xia_user_info uif ON tc.author=uif.uid";
        $res = $tabTaskComment->table($table)->field($field)->join($join)->where("tc.taskid=" . $taskid . " and tc.status=1")->select();
        if ($res) {
            ajaxs(1, '', $res);
        } else {
            ajaxf("获取失败！");
        }
    }

    //删除一条评论
    public function delComment()
    {
        $cid = I('comment'); //评论id
        //权限校验
        //是否自己写的评论
        $tabTC = M('taskcomment');
        $res = $tabTC->where("id=" . $cid . " and status=1")->find();
        if (!$res) {
            ajaxf();
        }
        if ($res['author'] != $this->uid) {
            ajaxf("无权限删除这条评论！");
        }
        //标记为删除
        $res['status'] = 0;
        $ret = $tabTC->save($res);
        if ($ret) {
            ajaxs();
        } else {
            ajaxf();
        }
    }

    //取消加急
    public function toggleLevel()
    {
        $task_id = I('id');

        $table = M('prjtask');
        $res = $table->where("id=" . $task_id . " and prjid=" . $this->userinfo['projectid'])->find();
        if ($res) {
            $res["level"] = $res["level"] == 1 ? 2 : 1;
            $ret = $table->save($res);
            if ($ret===FALSE) {
                ajaxf();
            }
            else{
                ajaxs();
            }
        }
        else {
            ajaxf();
        }
        
    }

    public function changeStatus()
    {
        $task_id = I('id');
        $status = I('status');
        $table = M('prjtask');
        $res = $table->where("id=" . $task_id . " and prjid=" . $this->userinfo['projectid'])->find();
        if ($res) {
            //当uptime为NULL时，认为执行者尚未认领，一旦认领，则创建者和执行者都能修改，且会更新修改时间
            if ($res['fromwho']==$this->uid && !$res['uptime']) {
                ajaxf("指定的任务执行者尚未认领，不能由发布者修改任务状态");
            }
            $res["process"] = $status;
            $res['modifier'] = $this->uid;
            $res['uptime'] = time();
            $ret = $table->save($res);
            if ($ret===FALSE) {
                ajaxf();
            }
            else {
                //通知
                $logarr = array();
                $now = time();
                //发布者修改状态
                $rcr = 0;
                $tab = "";
                if ($res['fromwho']==$this->uid) {
                    $rcr = $res['exer'];
                    $tab = "#tabMyPublish";
                }
                //执行者修改状态
                if ($res['exer']==$this->uid) {
                    $rcr = $res['fromwho'];
                    $tab = "#tabMyTasks";
                }
                $notice = "<td>任务[<b>".$res['title']."</b>]被用户[<a href='".  genTalentURL($this->uid)."'><b>".$this->userinfo['nickname']."</b></a>]修改成[<b>".$this->TASK_STATUS[$status]."</b>]状态，请知悉。</td>";
                $notice = logPost($notice, U("Task/main@".getDomain()).$tab, $now, "去看看");
                pushLog($logarr, $rcr, $notice, $now);
                //邮件通知
                $rcrinfo = M()->table(array('xia_user'=>'u'))->field("u.email,u.lastime")->join("left join xia_user_info uif on u.id=uif.uid")->where('u.id='.$rcr)->find();
                if ($rcrinfo['lastime']<$now-\App\Common\Constvars::MAIL_NOTICE_TIME) {
                    $body = "任务[".$res['title']."]被用户[".$rcrinfo['nickname']."]修改成[".$this->TASK_STATUS[$status]."]状态，请登陆<a href='".U("Task/main@".getDomain())."$tab'>般若虾-虚拟办公室</a>了解。";
                    $body = mailPostfix($body);
                    mailqueue_push($rcrinfo['email'], $rcrinfo['nickname'], '您有一个任务状态更改', $body);
                }
                //通知被@的人
                $tabTaskat = M('taskat');
                $join1 = "left join xia_user u on xia_taskat.towho=u.id";
                $join2 = "left join xia_user_info uif on xia_taskat.towho=uif.uid";
                $field = "u.email,u.lastime,uif.uid,uif.nickname";
                $bms = $tabTaskat->field($field)->join($join1)->join($join2)->where("taskid=$task_id")->select();
                if (is_array($bms)) {
                    foreach ($bms as $bm) {
                        $notice = "<td>您在其中被提到的任务[<b>".$res['title']."</b>]被用户[<a href='".  genTalentURL($bm['towho'])."'><b>".$this->userinfo['nickname']."</b></a>]修改成[<b>".$this->TASK_STATUS[$status]."</b>]状态，请知悉。</td>";
                        $notice = logPost($notice, U("Task/main@".getDomain())."#tabFollowMe", $now, "去看看");
                        pushLog($logarr, $bm['uid'], $notice, $now);
                        //邮件
                        if ($bm['lastime']<$now-\App\Common\Constvars::MAIL_NOTICE_TIME) {
                            $body = "您在其中被提到的任务[".$res['title']."]被用户[".$bm['nickname']."]修改成[".$this->TASK_STATUS[$status]."]状态，请登陆<a href='".U("Task/main@".getDomain())."#tabFollowMe'>般若虾-虚拟办公室</a>了解。";
                            $body = mailPostfix($body);
                            mailqueue_push($bm['email'], $bm['nickname'], '您有一个任务状态更改', $body);
                        }
                    }
                }
                M('office_log')->addAll($logarr);
                
                ajaxs();
            }
        }
        else {
            ajaxf();
        }
    }
    
    //统计数据
    public function statistic() {
        $tabTaskat = M('taskat');
        $tabTask = M('prjtask');
        //项目内任务总数
        $total = $tabTask->where('prjid='.$this->userinfo['projectid']." and status=1")
                ->count();
        //艾特我的
        $res3 = $tabTaskat->table(array('xia_taskat'=>'ta', 'xia_task'=>'t'))
                ->field('t.id,t.fromwho,t.exer,t.title,t.process,t.deadline,3 as rt')
                ->where('ta.towho='.$this->uid." and ta.taskid=t.id and t.prjid=".$this->userinfo['projectid']." and t.status=1")
                ->select();
        //我发布的
        $res2 = $tabTask->field('id,fromwho,exer,title,process,deadline,2 as rt')
                ->where('fromwho='.$this->uid." and prjid=".$this->userinfo['projectid']." and status=1")
                ->select();
        //我执行的
        $res1 = $tabTask->field('id.fromwho,exer,title,process,deadline,1 as rt')
                ->where('exer='.$this->uid." and prjid=".$this->userinfo['projectid']." and status=1")
                ->select();
        $all = array();
        if ($res1 && $res2 && $res3) {
            $all = array_merge($res1,$res2,$res3);
        }
        else if ($res1 && $res2) {
            $all = array_merge($res1,$res2);
        }
        else if ($res1 && $res3) {
            $all = array_merge($res1,$res3);
        }
        else if ($res2 && $res3) {
            $all = array_merge($res2,$res3);
        }
        else if ($res1) {
            $all = $res1;
        }
        else if ($res2) {
            $all = $res2;
        }
        else if ($res3) {
            $all = $res3;
        }
        usort($all, function($l, $r){
            if ($l['deadline']==$r['deadline']) {
                return 0;
            }
            return ($l['deadline']>$r['deadline'])?-1:1;
        });
        ajaxs(1, array('total'=>$total,'list'=>$all));
    }
}
